<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>组件三大属性</title>
  <style>
      .box {
          width: 100px;
          height: 100px;
          background-color: #bfa;
          margin: 0 auto;
      }
  </style>
</head>
<body>
<div id="root"></div>
<div id="root2"></div>
</body>
<script src="js/old/babel.min.js"></script>
<script src="js/old/react.development.js"></script>
<script src="js/old/react-dom.development.js"></script>
<script src="js/old/prop-types.js"></script>
<script type="text/babel">
  // 类组件
  class Weather extends React.Component {
    // 默认属性
    static defaultProps = {
      name: "", age: 18
    }
    // 属性类型限定
    static propTypes = {
      name: PropTypes.string.isRequired,
      age: PropTypes.number.isRequired
    }
    // 初始化状态
    state = {
      isHot: false, wind: '微风'
    }

    // ref容器
    h2 = React.createRef()

    // 事件处理函数
    changeWeather = () => {
      const {isHot} = this.state;
      this.setState({isHot: !isHot})
      console.log(this.h2.current)
    }

    render() {
      const {isHot, wind} = this.state
      // this.props.children
      return (<div>
        <h2 ref={this.h2} onClick={this.changeWeather}>
          今天天气很 {isHot ? '炎热' : '凉爽'}, {wind}
        </h2>
        {this.props.children}
      </div>);
    }
  }

  ReactDOM.render(<Weather>
    <div className="box"></div>
  </Weather>, document.getElementById('root'))

  // 函数组件: 更简洁一些
  const Count = props => {
    const [value, setValue] = React.useState(0)
    const increment = () => (setValue(value + 1))
    return (
      <React.Fragment>
        <h2>当前值: { value } </h2>
        <button onClick={increment}>increment</button>
        <p>slot with props.children { props.children}</p>
        <p>{props.a}</p>
      </React.Fragment>
    )
  }

  Count.propTypes = {
    a: PropTypes.string.isRequired,
  }
  Count.defaultProps = {
    a: 'default'
  }
  ReactDOM.render(<Count a="1">abc</Count>, document.getElementById('root2'))
</script>
</html>